//
//  TipBaseInfoView.m
//  TipsProject
//
//  Created by Suny on 15/5/23.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "TipBaseInfoView.h"
#import "MGLine.h"
#import "TipItemView.h"
#import "MGTableBoxStyled.h"
#import "TipImageItem.h"
#import "UIImageView+WebCache.h"
#import "TipInfoToolBar.h"

#define kFirstImageHeight 200


@interface UserInfoView:MGBox

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *nameLabel;

@property (nonatomic, strong)UIImageView *leftImageView;
@property (nonatomic, strong)UIImageView *rightImageView;

@end

@implementation UserInfoView


- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{
		[self addSubview:self.leftImageView];
		[self addSubview: self.rightImageView];
		[self addSubview:self.nameLabel];
		[self addSubview:self.titleLabel];
	}
	return self;
}


- (void)setTitle:(NSString *)title
{
    self.titleLabel.text = title;

}

- (void)setLeftImageUrlString:(NSString *)imageUrlString
{
    [self.leftImageView setQNImageWithURL:[NSURL URLWithString:imageUrlString]
                          placeholderImage:[UIImage imageNamed:kDefaultHeaderImage]];
}


- (UILabel *)nameLabel
{
	if (!_nameLabel)
	{
		_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.leftImageView.left - 10,
																self.leftImageView.bottom + 5,
																self.leftImageView.width + 20,
																20)];
		_nameLabel.font = [UIFont systemFontOfSize:16];
		_nameLabel.backgroundColor = [UIColor clearColor];
		_nameLabel.textAlignment = NSTextAlignmentCenter;
		
	}
	return _nameLabel;
}


- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.leftImageView.right + 20,
                                                                10,
                                                                self.width - 200,
                                                                self.height - 20)];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.backgroundColor = [UIColor clearColor];

    }
    return _titleLabel;
}

- (UIImageView *)rightImageView
{
    if (!_rightImageView)
    {
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.width - 30,
                                                                        0,
                                                                        16,
                                                                        40)];
        _rightImageView.centerY = (CGFloat) (self.height/2.0);
		
		_rightImageView.image = [UIImage imageNamed:@"2_1_arrowRight_blue"];
    }
    return _rightImageView;
}

- (UIImageView *)leftImageView
{

    if (!_leftImageView)
    {
        CGFloat imageSize = 40;
		
        _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30,
                                                                        0,
                                                                        imageSize,
                                                                        imageSize)];
        _leftImageView.centerY = (CGFloat) (self.height/2.0) - 10;

	}
    return _leftImageView;
}


@end


@interface TipBaseInfoView ()
@property (nonatomic, strong)TipInfo *baseInfo;
@property (nonatomic, strong)TipInfoToolBar *toolBar;

@property (nonatomic, strong)UILabel *timeLabel;
@property (nonatomic, strong)UILabel *topicNameLabel;
@property (nonatomic, strong)UIImageView *imageView;


@end
@implementation TipBaseInfoView


- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{
		
	}
	return self;
}

- (void)setup 
{
	
	// background
	self.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.95 alpha:1];
	
//	// shadow
//	self.layer.shadowColor = [UIColor colorWithWhite:0.12 alpha:1].CGColor;
//	self.layer.shadowOffset = CGSizeMake(0, 0.5);
//	self.layer.shadowRadius = 1;
//	self.layer.shadowOpacity = 1;
}

+ (TipBaseInfoView *)tipBaseInfoViewWithTipInfo:(TipInfo *)baseInfo withSize:(CGSize)size
{
	
	// box with photo number tag
	TipBaseInfoView *box = [TipBaseInfoView boxWithSize:size];
	[box setup];
	
	box.backgroundColor = [UIColor clearColor];
	
	box.baseInfo = baseInfo;
	box.contentMode = MGLayoutStackHorizontalWithWrap;
	
	
	// do the photo loading async, because internets
	__weak typeof(box) weakBox = box;
	box.asyncLayoutOnce = ^{
		[weakBox loadItemData];
	};
	
	return box;
	
}




- (void) loadItemData
{
    WEAKSELF
	dispatch_async(dispatch_get_main_queue(), ^{
        STRONGSELF
		// intro section
		MGTableBoxStyled *menu = MGTableBoxStyled.box;
		
		[menu setMargin:UIEdgeInsetsZero];
		[strongSelf.boxes addObject:menu];
		
		// header line
		MGLine
		*header = [MGLine lineWithLeft:@""
								 right:strongSelf.baseInfo.categoryName
								  size:CGSizeMake(strongSelf.width, 44)];
		header.layer.borderWidth = 0;
		header.backgroundColor = [UIColor clearColor];
		header.layer.cornerRadius = 0;
		header.layer.shadowPath = nil;
		//	header. 
		header.font = [UIFont fontWithName:kBoldFontName size:16];
		header.textColor = [UIColor colorWithHexString:@"ff7f67"];
		[menu.topLines addObject:header];
		

		TipImageItem *imageItem = [[TipImageItem alloc] init];
		imageItem.imageUrl = self.baseInfo.firstImageUrlString ;
		
		TipItemView *imageView = [TipItemView tipItemViewWithTipItem: imageItem
														 withSize:CGSizeMake(strongSelf.width, kFirstImageHeight)];
		imageView.needClips = YES;
		[imageView setMargin:UIEdgeInsetsZero];
		
		
		[strongSelf.boxes addObject:imageView];
		
//userinfo
		UserInfoView *userInfoView = [UserInfoView boxWithSize: CGSizeMake(strongSelf.width, 80)];
		[userInfoView setTitle:self.baseInfo.title];
		userInfoView.backgroundColor = [UIColor whiteColor];
		[userInfoView setLeftImageUrlString:self.baseInfo.memberPhoto];
		userInfoView.nameLabel.text = self.baseInfo.memberNickName;
        [userInfoView.nameLabel sizeToFit];
		userInfoView.nameLabel.centerX = userInfoView.leftImageView.centerX;
		[strongSelf.boxes addObject:userInfoView];
		
		
		// toolBar
		MGBox* photosGrid = [MGBox boxWithSize:CGSizeMake(self.width,80)];
		photosGrid.contentLayoutMode = MGLayoutStackHorizontalWithWrap;		
		[strongSelf.boxes addObject:photosGrid];

        _toolBar = [TipInfoToolBar tipInfoToolBarWithTipInfo:self.baseInfo                 
													 andSize: CGSizeMake(strongSelf.width, 50)]  ;
		
		_toolBar.toolBarDelegate = self.toolBarDelegate;

		_toolBar.backgroundColor = [UIColor whiteColor];
        [strongSelf.boxes addObject:_toolBar];
        [menu layout];
		[strongSelf layout];
		
		menu.layer.shadowPath = nil;	
	// shadow
		menu.layer.shadowColor = [UIColor clearColor].CGColor;
		menu.layer.shadowOffset = CGSizeMake(0, 0);
		menu.layer.shadowRadius = 0;
		menu.layer.shadowOpacity = 0;
		menu.layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tip_detail_background"]].CGColor;

	});
	
}
@end
